﻿using System.Windows.Forms;
using GeoSketch2.Core;

namespace GeoSketch2.Archaeo
{
    /// <summary>
    /// The tool to snap to an Point2D
    /// </summary>
    public class TSnapPoint : BaseCaptureTool
    {
        #region Constructor
        /// <summary>
        /// The tool to snap to an point, or a vertex
        /// </summary>
        /// <param name="Viewport">The viewport to draw to</param>
        /// <param name="MouseButtonCapturedTool">The mouse button of the captured tool</param>
        public TSnapPoint(BaseViewport Viewport, MouseButtons MouseButtonCapturedTool)
            : base(Viewport, MouseButtonCapturedTool, "Point snap")
        {
        } 
        #endregion

        #region Inputs
        /// <summary>
        /// The method called when the mouse button is down
        /// </summary>
        public override void MouseDown(MouseEventArgs MouseArgs)
        {
            double is_near_distance_object_coordinates = Viewport.Object2ImageTransformation.InverseTransform(Core.Vars.Setup.Tools.TSelection.IsNearDistance);
            Point2D64D clic_object_coordinates = Viewport.Object2ImageTransformation.InverseTransform(MouseArgs.Location);
            Point2D64D PointFound = null;

            foreach (BaseDrawableObject obj in Viewport.GeoObjects)
            {
                if (obj.Layer.Visible)
                {
                    PointFound = obj.NearestPoint(clic_object_coordinates, is_near_distance_object_coordinates);

                    if (PointFound != null)
                    {
                        CapturedTool.ExternalPointSent(PointFound);
                        RefreshViewport();
                        break;
                    }
                }
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// The method to be used to send the first message to the user, just after beeing selected
        /// </summary>
        public override void SendFirstMessage()
        {
            SendMessage("Click a point to snap to it");
        } 
        #endregion
    }
}
